<?php
// +----------------------------------------------------------------------
// | Iecms [ Make the most exquisite CMS ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009~2019 http://www.iemoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: iemoo <i@iemoo.com>
// +----------------------------------------------------------------------
// | DateTime: 2019/11/19 11:08
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\model\LinksType as LinksTypeModel;

use app\admin\validate\LinksType as LinksTypeValidate;
use think\exception\ValidateException;

use think\facade\Request;
use think\facade\View;
use think\facade\Config;

/**
 * 链接类型管理
 * Class AdminUser
 * @package app\admin\controller
 */
class LinksType extends Base
{
 
    /**
     * 链接类型管理
     * @return mixed
     */
    public function index()
    {
        $links_type_list = LinksTypeModel::select();
		View::assign('list',$links_type_list);
		return View::fetch('index');
    }

    /**
     * 添加链接类型
     * @return mixed
     */
    public function add()
    {

		if (Request::isPost()) {

            $data = Request::only(['name', 'description', 'sort', 'status']);

            try {
				validate(LinksTypeValidate::class)->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			if (LinksTypeModel::create($data)) {
				$this->success('保存成功',url('admin/LinksType/index'));
			} else {
				$this->error('保存失败');
			}

        }

		return View::fetch('add');
    }


    /**
     * 编辑链接类型
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {

		if (Request::isPost()) {

            $data = Request::only(['id', 'name', 'description', 'sort', 'status']);

            try {
				validate(LinksTypeValidate::class)->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			if (LinksTypeModel::update($data, ['id' => $id])) {
				$this->success('更新成功',url('admin/LinksType/index'));
			} else {
				$this->error('更新失败');
			}

        }

        $links_type = LinksTypeModel::find($id);

		View::assign('info',$links_type);
		return View::fetch('edit');
    }

    /**
     * 删除链接类型
     * @param $id
     */
    public function delete($id)
    {

		$links_type = LinksTypeModel::with('links')->find($id);
		$res = $links_type->together(['links'])->delete();

        if ($res != false) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}